<template>
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="110px">
        <el-row :gutter="24">
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-form-item label="上传功能模块" prop="moduleName">
              <el-input
                v-model="queryParams.moduleName"
                clearable
                placeholder="请输入上传功能模块"
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-form-item label="附件显示名称" prop="originalName">
              <el-input
                v-model="queryParams.originalName"
                clearable
                placeholder="请输入附件显示名称"
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-form-item label="附件保存文件名" prop="fileName">
              <el-input
                v-model="queryParams.fileName"
                clearable
                placeholder="请输入附件保存文件名"
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-form-item label="附件类型" prop="fileExt">
              <el-input
                v-model="queryParams.fileExt"
                clearable
                placeholder="请输入附件类型"
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-form-item label="业务ID" prop="businessKey">
              <el-input
                v-model="queryParams.businessKey"
                clearable
                placeholder="请输入业务ID"
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="8">
            <el-row type="flex" justify="end">
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              <el-button icon="el-icon-search" size="small" type="cyan" @click="handleQuery">搜索</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['attachment:base:add']"
            icon="el-icon-plus"
            size="mini"
            type="primary"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['attachment:base:edit']"
            :disabled="single"
            icon="el-icon-edit"
            size="mini"
            type="success"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['attachment:base:remove']"
            :disabled="multiple"
            icon="el-icon-delete"
            size="mini"
            type="danger"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['attachment:base:export']"
            icon="el-icon-download"
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="attachmentList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column align="center" label="ID" prop="id"/>
        <el-table-column align="center" label="上传功能模块" prop="moduleName"/>
        <el-table-column align="center" label="上传控件标识" prop="uploaderId"/>
        <el-table-column align="center" label="业务ID" prop="businessKey"/>
        <el-table-column align="center" label="附件显示名称" prop="originalName"/>
        <el-table-column align="center" label="附件大小" prop="fileSizeStr"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['attachment:base:edit']"
              icon="el-icon-edit"
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              v-hasPermi="['attachment:base:remove']"
              icon="el-icon-delete"
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>
    <edit ref="editDialog" @refreshDataList="getList"></edit>
  </div>
</template>

<script>
import {delAttachment, exportAttachment, listAttachment} from '@/api/attachment'
import Edit from './edit'

export default {
  name: 'Attachment',
  components: {Edit},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 附件管理表格数据
      attachmentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moduleName: null,
        originalName: null,
        fileName: null,
        fileExt: null,
        fileSize: null,
        businessKey: null,
        uploaderId: null,
        md5: null,
        sha1: null
      }
    }

  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询附件管理列表 */
    getList() {
      this.loading = true
      listAttachment(this.queryParams).then(response => {
        this.attachmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.$refs['editDialog'].reset()
      this.$refs['editDialog'].open = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.$refs['editDialog'].reset()
      this.$refs['editDialog'].open = true
      const id = row.id || this.ids
      this.$refs['editDialog'].init(id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除附件管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delAttachment(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function () {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有附件管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return exportAttachment(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function () {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item {
  width: 100%;
  display: flex;
}
</style>
<style lang="scss" >
.el-form-item--medium {
  .el-form-item__content {
    flex: 1;
  }
}
</style>
